package com.hongdee.base.imp.service.impl;

import com.hongdee.base.entity.BaseEntity;
import com.hongdee.base.imp.dao.IDataEntityPropertyDao;
import com.hongdee.base.imp.dao.IExcelSchemePropertyDao;
import com.hongdee.base.imp.entity.TDataEntityProperty;
import com.hongdee.base.imp.entity.TExcelSchemeProperty;
import com.hongdee.base.imp.service.IExcelSchemePropertyService;
import com.hongdee.base.util.EntityFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 名  称:ExcelSchemePropertyServiceImpl
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.base.imp.service.impl
 * 作  者:My
 * 日  期:2017/2/15 13:50
 */
@Service
public class ExcelSchemePropertyServiceImpl implements IExcelSchemePropertyService {


    @Autowired
    private IExcelSchemePropertyDao excelSchemePropertyDao;


    @Override
    public Map page(String start, String limit, String schemeId) throws Exception {
        String sql = "select * from T_EXCEL_SCHEME_PROPERTY where scheme_id = ? ";
        return this.excelSchemePropertyDao.page(sql, new Object[]{schemeId}, start, limit);
    }


    @Override
    public void saveOrUpdateProperty(TExcelSchemeProperty tes) throws Exception {
        //判断非空是否填写数据
        if(tes.getFieldEmpty().equals("")){
            throw new Exception("请选择字段是否为空!!");
        }
        if("".equals(tes.getFieldType())){
            throw new Exception("请选择字段类别!!");
        }
        if(!"".equals(tes.getFieldLen())){
            int i=Integer.valueOf(tes.getFieldLen());
            if(i<=0){
                throw new Exception("字段长度不能小于0!!");
            }
        }else{
            throw new Exception("请输入字段长度!!");
        }
        excelSchemePropertyDao.saveOrUpdate(tes);
    }

    /**
     * 初始化导入方案属性
     *
     * @param schemeId
     * @param dataEntityId
     * @throws Exception
     */
    @Override
    public void initPropertyInfo(String schemeId, String dataEntityId) throws Exception {
        if (StringUtils.isEmpty(schemeId) || StringUtils.isEmpty(dataEntityId))
            throw new Exception("请选择有效方案");
        /***
         * 根据 dataEntityID 查询对应的属性信息
         */
        List<Map<String, Object>> propsList = this.excelSchemePropertyDao.listByTableName("T_DATA_ENTITY_PROPERTY", "entity_id", dataEntityId);
        if (null == propsList || propsList.size() == 0) {
            throw new Exception("无效的导入实体，请先配置导入实体");
        }

        List<TExcelSchemeProperty> schPropsList = new ArrayList<TExcelSchemeProperty>();
        TExcelSchemeProperty esp = null;
        for (int i = 0; i < propsList.size(); i++) {
            Map temp = propsList.get(i);
            esp = new TExcelSchemeProperty();
            //设置模板 ID
            esp.setSchemeId(schemeId);
            //设置属性 ID
            esp.setPropertyId(String.valueOf(temp.get("id")));
            //设置展示顺序
            esp.setDispIndex(i);

            esp.setPropertyName(String.valueOf(temp.get("propertyName")));
            esp.setPropertyTitle(String.valueOf(temp.get("propertyTitle")));
            esp.setStatus(1);
            schPropsList.add(esp);
        }
        //保存之前，先清除老的数据
        String delHql = "delete from TExcelSchemeProperty where schemeId=:schemeId ";
        Map params = new HashMap();
        params.put("schemeId", schemeId);
        excelSchemePropertyDao.update(delHql, params);
        excelSchemePropertyDao.saveOrUpdateList(schPropsList);
    }
}
